<template>
	<view class="headerput">
		<view class="header">
			<image :src="imgurl" mode="widthFix"></image>
		</view>
		
		<view class="footer">
			<view class="left" @click="changeimg">
				<text>重新选择</text>
			</view>
			<view class="right" @click="putimg">
				<text>确认上传</text>
			</view>
		</view>
		 
	</view>
</template>

<script>
	export default{
		data(){
			return {
				imgurl:'',
				userdata:{}
			}
		},
		onLoad(e){
			// console.log(e.url)
			this.imgurl=e.url
			this.userdata=uni.getStorageSync("userinfo")
		},
		methods:{
			//确认上传
			putimg(){ 
				 uni.uploadFile({
				            url: `http://127.0.0.1:3000/user/putimg?id=${this.userdata.id}`, 
				            filePath: this.imgurl,
				            name: 'file',
				            success: (res) => {
								let data1=JSON.parse(res.data)
				                uni.showToast({
									title:'上传成功',
									duration:1500
								}),
								this.userdata.userimg=data1.userimg
								uni.setStorageSync("userinfo",this.userdata)
								uni.redirectTo({
									url:"/pages/me/meinfo"
								})
								
				            }
				        });
			},
			//重新选择
			changeimg(){
				var that=this
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: function (res) {
						that.imgurl=res.tempFilePaths[0] 
				       
				    }
				});
			}
			
		}
	}
</script>

<style lang="less" scoped>
	.headerput{
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		padding: 10upx;
		.header{
			flex: 5;
			display: flex;
			justify-content: center;
			align-items: center;
			border: 1px solid rgba(0,0,0,.4);
			image{
				width: 80%;
			}
		}
		
		.footer{
			width: 100vw;
			flex: 1;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-top: 1px solid #ccc;
			.left{
				width:200upx;
				text-align: center;
				text{
					font-size: 32upx;
					font-weight: 600;
				}
			}
			.right{
				width:200upx;
				text-align: center;
				text{
					font-size: 32upx;
					font-weight: 600;
				}
			}
		}
	}
</style>
